package com.qingzhuge.common.logger;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.qingzhuge.common.utils.IpUtils;
import com.qingzhuge.common.utils.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * @author : zero.xiao
 * @description : logback 打印ip地址
 * @date :2018/10/11 0011 下午 14:57
 * @since : 1.0.0
 * @modified :
 */
public class IpConvert extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        String ip = null;
        HttpServletRequest request = WebUtil.getRequest();
        if (null != request) {
            ip = IpUtils.getIpAddr(request);
        }else {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        return ip;
    }
}